package com.zksk.admin.controller.portal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.api.domain.portal.vo.ViolateRequestVo;
import com.zksk.admin.service.portal.ViolateService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.Violate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 违规资讯信息表
 */
@Slf4j
@RestController
@RequestMapping("/portal/violate")
@Api(value = "ViolateController", tags = "信息公示相关接口")
public class ViolateController extends BaseController {

    @Autowired
    private ViolateService violateService;


    @Log(title = "违规资讯信息列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "违规资讯信息列表")
    @PostMapping("/list")
    public R<Page<Violate>> list(@RequestBody ViolateRequestVo requestVo){
        //构建条件
        LambdaQueryWrapper<Violate> wrapper = new LambdaQueryWrapper<Violate>()
                .like(StringUtils.isNotBlank(requestVo.getTitleName()), Violate::getTitleName, requestVo.getTitleName())
                .like(StringUtils.isNotBlank(requestVo.getViolateName()), Violate::getViolateName, requestVo.getViolateName())
                .eq(StringUtils.isNotBlank(requestVo.getViolateType()), Violate::getViolateType, requestVo.getViolateType())
                .eq(StringUtils.isNotBlank(requestVo.getStatus()), Violate::getStatus, requestVo.getStatus())
                .eq(StringUtils.isNotBlank(requestVo.getNoticeDept()), Violate::getNoticeDept, requestVo.getNoticeDept())
                .eq(StringUtils.isNotBlank(requestVo.getPunishBasis()), Violate::getPunishBasis, requestVo.getPunishBasis())
                .eq(StringUtils.isNotBlank(requestVo.getPunishResult()), Violate::getPunishResult, requestVo.getPunishResult())
                .orderByAsc(Violate::getCreateTime);

        //构建分页
        Page<Violate> page = violateService.page(
                new Page<>(requestVo.getPageNum(), requestVo.getPageSize()), wrapper);

        return R.ok(page, "获取违规资讯信息列表成功");
    }

    /**
     * 根据id查询信息
     * @param violateId
     * @return
     */
    @GetMapping(value = "/{violateId}")
    @ApiOperation(value = "根据id查询信息")
    public R<Violate> select(@PathVariable Long violateId) {
        Violate violateById = violateService.getById(violateId);
        return R.ok(violateById, "查看详情信息成功");
    }

    /**
     * 删除违规资讯信息
     * @param ids
     * @return
     */
    @Log(title = "删除违规资讯信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除违规资讯信息(逻辑删除)")
    public R<String> remove(@PathVariable List<Long> ids) {
        violateService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }

    /**
     * 新增违规记录
     * @param violate
     * @return
     */
    @Log(title = "新增违规记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增违规记录")
    public R<Violate> add(@Validated(Insert.class) @RequestBody Violate violate) {
        violate.setCreateBy(SecurityUtils.getUsername());
        violateService.save(violate);
        return R.ok(violate,"新增成功");
    }

    /**
     * 更新违规记录
     * @param violate
     * @return
     */
    @Log(title = "更新违规记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "更新违规记录")
    public R<Violate> update(@Validated(Update.class) @RequestBody Violate violate) {
        violate.setUpdateBy(SecurityUtils.getUsername());
        violateService.updateById(violate);
        return R.ok(violate,"保存成功");
    }

}
